package models

import (
	__ "appointment-srv/basic/proto"
	"gorm.io/gorm"
)

type Order struct {
	gorm.Model
	UserId     int    `gorm:"type:int;comment:'用户id'"`
	PackageId  int    `gorm:"type:int;comment:'套餐id'"`
	HospitalId int    `gorm:"type:int;comment:'医院id'"`
	OrderSn    string `gorm:"type:varchar(255);comment:'订单编号的'"`
	//ReserveDate   time.Time `gorm:"type:datetime;comment:'预约日期'"`
	Amount        float64 `gorm:"type:decimal(10,2);comment:'订单金额1'"`
	PaymentMethod int     `gorm:"type:int;comment:'支付方式 1 微信 2 支付宝'"`
	Status        int     `gorm:"type:int;comment:'订单状态 0 - 待支付，1 - 已支付，2 - 已取消，3 - 已完成，4 - 已退款'"`
	Storck        int     `gorm:"type:int;comment:'订单库存'"`
	//PaymentTime   time.Time `gorm:"type:datetime;comment:'支付时间1'"`
	//Canceltime    time.Time `gorm:"type:datetime;comment:'取消时间'"`
}
type OrderFlow struct {
	gorm.Model
	PackageId  int    `gorm:"type:int;comment:'套餐id'"`
	HospitalId int    `gorm:"type:int;comment:'医院id'"`
	OrderSn    string `gorm:"type:varchar(255);comment:'订单编号的'"`

	Amount        float64 `gorm:"type:decimal(10,2);comment:'订单金额1'"`
	PaymentMethod int     `gorm:"type:int;comment:'支付方式 1 微信 2 支付宝'"`
	ThirdPartyNo  int     `gorm:"type:int;comment:'订单状态 0 - 待支付，1 - 已支付，2 - 已取消，3 - 已完成，4 - 已退款'"`
	Storck        int     `gorm:"type:int;comment:'订单库存'"`
}

func (o *Order) Create(db *gorm.DB) error {
	err := db.Create(&Order{}).Error
	return err

}

func (o *Order) Del(db *gorm.DB, id int) error {
	err := db.Model(&Order{}).Where("id = ?", id).Delete(id).Error
	return err

}
func (o *Order) Update(db *gorm.DB, id int) error {
	err := db.Model(&Order{}).Where("is = ?", id).Updates("storck").Error
	return err

}
func (o *Order) GetList(db *gorm.DB, page int, pagesize int) (list []*__.UserOrderList, err error) {
	err = db.Model(&Order{}).Scopes(Paginate(page, pagesize)).Order("created_at desc").Find(&list).Error
	return
}

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {

		if page <= 0 {
			page = 1
		}
		switch {
		case pageSize > 100:
			pageSize = 100
		case pageSize <= 0:
			pageSize = 10
		}

		offset := (page - 1) * pageSize
		return db.Offset(offset).Limit(pageSize)
	}
}
